package servlets;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
import classes.Teste_Acesso_BD;
import classes.Utilizador;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import utilities.Utilitario;

/**
 *
 * @author Mafalda
 */
@WebServlet(name = "ServletLogin", urlPatterns = {"/ServletLogin"})
public class ServletLogin extends HttpServlet {

    HttpSession session = null;
    private Utilitario utilitario = new Utilitario();

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        try {


            Utilizador u = utilitario.devolveUtilizador(request.getParameter("nome"), request.getParameter("password"));
            //System.out.print(u);

            if (u != null) {
                

                /*
                 * out.println("<html>"); out.println("<head>");
                 * out.println("<title>Servlet Servlet01</title>");
                 * out.println("</head>"); out.println("<body>");
                 * out.println("<h1>Servlet Servlet01 at " +
                 * request.getContextPath() + "</h1>"); out.println("<br/>id: "
                 * + u.getId() + "<br/>Username: " + u.getUsername() +
                 * "<br/>Password: " + u.getPassword()); out.println("</body>");
                 * out.println("</html>");
                 */
                
                session = request.getSession();
                session.setAttribute("username", request.getParameter("nome"));
               
                response.sendRedirect("PaginaLogada.jsp");
                

            } else {
                response.sendRedirect("index.html");
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
        
        
        
        System.out.println("entei no doget");

        //Teste_Acesso_BD acesso = new Teste_Acesso_BD();


    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
        System.out.println("entei no dopost");
        /*
         * response.setContentType("text/html"); PrintWriter out =
         * response.getWriter(); String title = "Reading All Request
         * Parameters"; out.println(ServletUtilities.headWithTitle(title) +
         * "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=CENTER>" + title +
         * "</H1>\n" + "<TABLE BORDER=1 ALIGN=CENTER>\n" + "<TR
         * BGCOLOR=\"#FFAD00\">\n" + "<TH>Parameter Name<TH>Parameter
         * Value(s)");
         *
         * Enumeration paramNames = request.getParameterNames(); while
         * (paramNames.hasMoreElements()) { String paramName = (String)
         * paramNames.nextElement(); out.println("<TR><TD>" + paramName +
         * "\n<TD>"); String[] paramValues =
         * request.getParameterValues(paramName); if (paramValues.length == 1) {
         * String paramValue = paramValues[0];
         *
         * if (paramValue.length() == 0) { out.println("<I> No value<I>"); }
         * else { out.print(paramValue); } } else { out.println("<UL>"); for
         * (int i = 0; i < paramValues.length; i++) { out.println("<LI>" +
         * paramValues[i]); } out.println("</UL>"); } }
         * out.println("</TABLE>\n</BODY></HTML>");
         */
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
